package com.wft.chepter16.thread;

import sun.reflect.misc.FieldUtil;

import java.sql.Time;
import java.util.concurrent.*;

public class T03ImplCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        TimeUnit.SECONDS.sleep(5);
        return 1024;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
//        Thread thread = new Thread(new T03ImplCallable())

        /**
         * FutureTask类实现了RunnableFuture接口
         * RunnableFuture继承了Runnable和Future
         */
        FutureTask future = new FutureTask(new T03ImplCallable());
        Thread thread = new Thread(future);
        thread.start();

        TimeUnit.SECONDS.sleep(1);
        // 尝试取消callable任务,mayInterruptIfRunning是否中断正在运行的线程
        future.cancel(false);

        // get 在获取结果之前一直阻塞
        System.out.println(future.get());
        // 获取结果超过2s抛出异常
//        System.out.println(future.get(4,TimeUnit.SECONDS));

    }
}
